package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.AlgsMessage;
import com.ruoyi.system.service.AlgsMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.AlgsInspectionRecord;
import com.ruoyi.system.service.IAlgsInspectionRecordService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 巡查记录Controller
 * 
 * @author ruoyi
 * @date 2023-03-21
 */
@RestController
@RequestMapping("/record")
public class AlgsInspectionRecordController extends BaseController
{
    @Autowired
    private IAlgsInspectionRecordService algsInspectionRecordService;
    @Autowired
    private AlgsMessageService algsMessageService;


    /**
     * 推送
     */
    @RequiresPermissions("record:record:add")
    @Log(title = "推送到站内信", businessType = BusinessType.INSERT)
    @GetMapping ("/addxin/{inspectionRecordId}")
    public AjaxResult addxin(@PathVariable Long inspectionRecordId){
        AlgsInspectionRecord algsInspectionRecord = algsInspectionRecordService.selectAlgsInspectionRecordByInspectionRecordId(inspectionRecordId);
        final String name = algsInspectionRecord.getInspectionRecordAdd();
        final String desc = algsInspectionRecord.getInspectionRecordDesc();
        AlgsMessage a = new AlgsMessage();
        a.setMessageSendDate(new Date());

        a.setMessageMessage(name+desc);
        return toAjax(algsMessageService.insert(a));

    }


    /**
     * 查询巡查记录列表
     */
    @RequiresPermissions("record:record:list")
    @GetMapping("/list")
    public TableDataInfo list(AlgsInspectionRecord algsInspectionRecord)
    {
        startPage();
        List<AlgsInspectionRecord> list = algsInspectionRecordService.selectAlgsInspectionRecordList(algsInspectionRecord);
        return getDataTable(list);
    }



    /**
     * 导出巡查记录列表
     */
    @RequiresPermissions("record:record:export")
    @Log(title = "巡查记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgsInspectionRecord algsInspectionRecord)
    {
        List<AlgsInspectionRecord> list = algsInspectionRecordService.selectAlgsInspectionRecordList(algsInspectionRecord);
        ExcelUtil<AlgsInspectionRecord> util = new ExcelUtil<AlgsInspectionRecord>(AlgsInspectionRecord.class);
        util.exportExcel(response, list, "巡查记录数据");
    }

    /**
     * 获取巡查记录详细信息
     */
    @RequiresPermissions("record:record:query")
    @GetMapping(value = "/{inspectionRecordId}")
    public AjaxResult getInfo(@PathVariable("inspectionRecordId") Long inspectionRecordId) {

        return success(algsInspectionRecordService.selectAlgsInspectionRecordByInspectionRecordId(inspectionRecordId));
    }

    /**
     * 新增巡查记录
     */
    @RequiresPermissions("record:record:add")
    @Log(title = "巡查记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AlgsInspectionRecord algsInspectionRecord) {
        algsInspectionRecord.setTypeId("300");
        return toAjax(algsInspectionRecordService.insertAlgsInspectionRecord(algsInspectionRecord));
    }

    @RequiresPermissions("record:record:add2")
    @Log(title = "巡查记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add2(@RequestBody AlgsInspectionRecord algsInspectionRecord) {
        algsInspectionRecord.setTypeId("301");

        return toAjax(algsInspectionRecordService.insertAlgsInspectionRecord(algsInspectionRecord));
    }

    /**
     * 修改巡查记录
     */
    @RequiresPermissions("record:record:edit")
    @Log(title = "巡查记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AlgsInspectionRecord algsInspectionRecord)
    {
        return toAjax(algsInspectionRecordService.updateAlgsInspectionRecord(algsInspectionRecord));
    }

    /**
     * 删除巡查记录
     */
    @RequiresPermissions("record:record:remove")
    @Log(title = "巡查记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{inspectionRecordIds}")
    public AjaxResult remove(@PathVariable Long[] inspectionRecordIds)
    {
        return toAjax(algsInspectionRecordService.deleteAlgsInspectionRecordByInspectionRecordIds(inspectionRecordIds));
    }
}
